In [1]:
from os import path

from astropy.constants import c
import astropy.coordinates as coord
import astropy.units as u
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('apw-notebook')
%matplotlib inline
from scipy.optimize import minimize

from comoving_rv.log import logger
from comoving_rv.db import Session, Base, db_connect
from comoving_rv.db.model import (Run, Observation, TGASSource, SimbadInfo, PriorRV,
                                  SpectralLineInfo, SpectralLineMeasurement)

In [2]:
base_path = '/Volumes/ProjectData/gaia-comoving-followup/'
db_path = path.join(base_path, 'db.sqlite')
engine = db_connect(db_path)
session = Session()

In [3]:
Halpha, = session.query(SpectralLineInfo.wavelength).filter(SpectralLineInfo.name == 'Halpha').one()

For now, only get observations that are done that have a Simbad RV already in the database


In [4]:
q = session.query(Observation).join(Run, SpectralLineMeasurement, PriorRV)
q = q.filter(Run.name == 'mdm-spring-2017')
q = q.filter(SpectralLineMeasurement.x0 != None)
q = q.filter(PriorRV.rv != None)
q.distinct().count()


Out[4]:
155

In [5]:
observations = q.all()

In [6]:
raw_offsets = np.zeros(len(observations)) * u.angstrom
all_sky_offsets = np.full((len(observations), 3), np.nan) * u.angstrom
true_rv = np.zeros(len(observations)) * u.km/u.s
meas_rv = np.zeros(len(observations)) * u.km/u.s
night_id = np.zeros(len(observations), dtype=int)
obs_time = np.zeros(len(observations))
airmass = np.zeros(len(observations))
hour_angle = np.zeros(len(observations))
color_by = np.zeros(len(observations))

for i,obs in enumerate(observations):
    print(obs.object, obs.filename_1d)
    
    # color_by[i] = obs.airmass
    # color_by[i] = obs.exptime
    
    obs_time[i] = np.sum(np.array(list(map(float, obs.time_obs.split(':')))) / np.array([1., 60., 3600.]))
    color_by[i] = obs_time[i]
    night_id[i] = obs.night
    airmass[i] = obs.airmass
    hour_angle[i] = obs.ha.degree

    x0 = obs.measurements[0].x0 * u.angstrom
    offset = (x0 - Halpha)
    raw_offsets[i] = offset
    
    sky_offsets = []
    for j,meas in enumerate(obs.measurements[1:]):
        sky_offset = meas.x0*u.angstrom - meas.info.wavelength
        if meas.amp > 16 and meas.std_G < 2 and meas.std_G > 0.3 and np.abs(sky_offset) < 4*u.angstrom:
            sky_offsets.append(sky_offset)
            all_sky_offsets[i,j] = sky_offset
            print(meas.info.name, meas.x0, meas.amp, sky_offset)
            
    sky_offsets = u.Quantity(sky_offsets)
    
    if len(sky_offsets) > 0:
        sky_offset = np.mean(sky_offsets)
    else:
        sky_offset = 0. * u.angstrom
        print("WARNING: not correcting with sky line")
    
    # sky_offset = -1.2*u.angstrom
    meas_rv[i] = (offset - sky_offset) / Halpha * c.to(u.km/u.s) + obs.v_bary
    true_rv[i] = obs.prior_rv.rv - obs.v_bary
    print(meas_rv[i] - true_rv[i])
    print()


1243-5711 1d_n1.0034.fit
[OI] 5577 5577.894895342597 106.64093466986985 0.5561953425967658 Angstrom
[OI] 6300 6300.722636191856 46.019564017326076 0.41863619185551215 Angstrom
-32.19755408050325 km / s

1243-1392 1d_n1.0035.fit
[OI] 5577 5577.99269502419 282.1828313360044 0.6539950241894985 Angstrom
[OI] 6364 6364.680487501012 20.442800447307768 0.9044875010122269 Angstrom
-62.98155832700147 km / s

392-10134 1d_n1.0038.fit
[OI] 5577 5578.035563976712 143.0788006557491 0.6968639767119384 Angstrom
[OI] 6300 6300.945222224586 45.1043976928295 0.6412222245862722 Angstrom
-41.33351110142242 km / s

1219-1353 1d_n1.0059.fit
[OI] 5577 5576.050638745876 908.1829559557491 -1.2880612541239316 Angstrom
[OI] 6300 6299.224065818754 24.21443888860573 -1.0799341812462444 Angstrom
-19.328214628804254 km / s

1254-1407 1d_n1.0089.fit
[OI] 5577 5576.667122907217 228.2631511246277 -0.6715770927830818 Angstrom
[OI] 6300 6299.212950861496 31.82249552509176 -1.0910491385038767 Angstrom
4.7081179516255744 km / s

733-6317 1d_n1.0113.fit
[OI] 5577 5576.237471612407 138.49581346940738 -1.1012283875934372 Angstrom
[OI] 6300 6299.095248346832 18.05314437059442 -1.2087516531682923 Angstrom
-41.65523502122717 km / s

729-1175 1d_n1.0114.fit
[OI] 5577 5576.0511350584275 281.8681612793559 -1.2875649415727821 Angstrom
[OI] 6300 6298.540271470228 22.419044143603443 -1.7637285297723793 Angstrom
17.752711647974742 km / s

465-186 1d_n1.0120.fit
[OI] 5577 5576.399234828938 290.18869643882374 -0.9394651710626931 Angstrom
[OI] 6300 6299.066794847245 39.34963474837453 -1.2372051527554504 Angstrom
-5.548297502169113 km / s

1178-1284 1d_n1.0128.fit
[OI] 5577 5576.108905962329 164.35372176259466 -1.2297940376711267 Angstrom
1.2282055904848193 km / s

HD155967 1d_n1.0136.fit
[OI] 5577 5576.069289027988 313.4310684109396 -1.2694109720123379 Angstrom
[OI] 6300 6299.007282571519 115.8253853367754 -1.2967174284813154 Angstrom
[OI] 6364 6362.032341829784 35.401854694790984 -1.7436581702158946 Angstrom
34.745688905025574 km / s

1453-1738 1d_n2.0023.fit
[OI] 5577 5576.693341552109 41.447449198663755 -0.6453584478913399 Angstrom
[OI] 6300 6300.563886274048 72.87581484680821 0.259886274047858 Angstrom
[OI] 6364 6363.918314723912 22.372772190948044 0.1423147239120226 Angstrom
-21.589512135650452 km / s

2260-3320 1d_n2.0027.fit
[OI] 5577 5576.531200719395 105.48068495014728 -0.8074992806050432 Angstrom
[OI] 6300 6300.677734963464 85.29864186706216 0.3737349634639031 Angstrom
[OI] 6364 6364.004876782401 25.42071845964093 0.22887678240113019 Angstrom
-29.455206522632892 km / s

2093-3350 1d_n2.0028.fit
[OI] 5577 5577.575870675486 75.71257743183621 0.23717067548568593 Angstrom
[OI] 6300 6301.9198393068455 78.08322115156928 1.6158393068453734 Angstrom
[OI] 6364 6364.93649943343 17.465198461358963 1.1604994334302319 Angstrom
-33.49057487166583 km / s

1515-3584 1d_n2.0051.fit
WARNING: not correcting with sky line
-63.57742775270037 km / s

1609-7364 1d_n2.0077.fit
[OI] 5577 5575.948628082854 710.572200607881 -1.3900719171460878 Angstrom
-11.215898057455977 km / s

1614-2052 1d_n2.0080.fit
[OI] 5577 5575.304519784499 292.64005989902887 -2.034180215501692 Angstrom
[OI] 6300 6299.663910634886 27.20248716595147 -0.6400893651143633 Angstrom
-16.987244997022316 km / s

1500-1804 1d_n2.0083.fit
[OI] 5577 5575.400325672099 142.21638328361166 -1.9383743279013288 Angstrom
19.812053594789745 km / s

1454-1739 1d_n2.0110.fit
[OI] 5577 5575.509430068983 339.84535191533587 -1.8292699310168246 Angstrom
[OI] 6300 6299.750777932129 48.351415149640985 -0.5532220678715021 Angstrom
[OI] 6364 6362.994310223275 23.18113510071079 -0.7816897767252158 Angstrom
36.34927294404558 km / s

1741-2283 1d_n2.0114.fit
[OI] 5577 5575.507619597368 60.0820501800943 -1.831080402632324 Angstrom
44.32432361527795 km / s

2273-3360 1d_n2.0125.fit
[OI] 5577 5575.338362285754 221.85247925165376 -2.000337714246598 Angstrom
[OI] 6300 6299.026217549964 26.048630764858885 -1.277782450036284 Angstrom
[OI] 6364 6362.93654214663 22.342160282633394 -0.8394578533698223 Angstrom
21.869759474248063 km / s

1821-2435 1d_n2.0137.fit
[OI] 5577 5575.151264657336 133.65207247258846 -2.187435342664685 Angstrom
[OI] 6300 6299.346522177851 33.89797978092841 -0.9574778221494853 Angstrom
29.80676709057424 km / s

2342-3491 1d_n2.0152.fit
[OI] 5577 5575.698210178726 47.80703112727669 -1.640489821274059 Angstrom
[OI] 6300 6298.8119559350025 16.111805782836978 -1.4920440649975717 Angstrom
54.41337432794139 km / s

1613-3003 1d_n2.0164.fit
[OI] 5577 5576.870793000388 102.28050131190793 -0.46790699961184146 Angstrom
[OI] 6300 6301.708652869097 25.520792452549806 1.4046528690969353 Angstrom
29.761721218764595 km / s

1613-2050 1d_n2.0165.fit
[OI] 5577 5577.563484680924 137.8776730510688 0.22478468092413095 Angstrom
[OI] 6300 6301.68461214305 35.49287911160398 1.3806121430498024 Angstrom
[OI] 6364 6364.889935466487 22.30399421136387 1.113935466487419 Angstrom
18.50868340767606 km / s

1790-2371 1d_n2.0168.fit
[OI] 5577 5577.400685519994 90.72012305260482 0.061985519993868365 Angstrom
40.94822405000134 km / s

HD140283 1d_n2.0170.fit
[OI] 5577 5577.24061581858 66.95856791567711 -0.09808418141983566 Angstrom
[OI] 6364 6365.015819930576 22.04759617987451 1.2398199305762319 Angstrom
47.44377703702338 km / s

2495-4212 1d_n3.0023.fit
[OI] 6300 6300.216105057771 216.4758239665558 -0.08789494222946814 Angstrom
[OI] 6364 6364.040795951305 109.04055303987306 0.2647959513051319 Angstrom
-0.179413998607302 km / s

2495-3799 1d_n3.0024.fit
[OI] 5577 5578.019327076363 178.3594831405137 0.6806270763627253 Angstrom
[OI] 6300 6300.841608878031 197.6939827701186 0.5376088780312784 Angstrom
[OI] 6364 6364.449766498345 80.05616535656979 0.6737664983447758 Angstrom
6.1710360886050495 km / s

2995-8632 1d_n3.0035.fit
[OI] 5577 5577.694666052258 133.4503556935825 0.35596605225782696 Angstrom
[OI] 6300 6301.111829839836 98.50754308379169 0.8078298398359038 Angstrom
[OI] 6364 6364.279395727718 46.414417277573406 0.5033957277182708 Angstrom
31.40248099709666 km / s

2561-4778 1d_n3.0040.fit
[OI] 5577 5577.752133487068 126.67638730664817 0.4134334870677776 Angstrom
[OI] 6300 6300.702348339738 65.43366941289601 0.39834833973782224 Angstrom
2.9947090619444197 km / s

HIP34511 1d_n3.0047.fit
[OI] 5577 5577.983923299052 20.653382665501784 0.6452232990513949 Angstrom
[OI] 6300 6300.052686754672 90.95722763110308 -0.2513132453277649 Angstrom
-0.04332889640286908 km / s

HIP36849 1d_n3.0049.fit
[OI] 5577 5577.163267509795 133.18501474356347 -0.17543249020491203 Angstrom
[OI] 6300 6300.0227424419745 140.81077966732343 -0.2812575580255725 Angstrom
-1697.504979073932 km / s

HIP36491 1d_n3.0050.fit
[OI] 6300 6300.423969869623 46.21050616927699 0.11996986962276424 Angstrom
9.366564685940887 km / s

HIP28671 1d_n3.0053.fit
[OI] 5577 5576.130406320073 87.98838427575353 -1.20829367992701 Angstrom
[OI] 6300 6299.869480333572 51.94469351042655 -0.4345196664280593 Angstrom
[OI] 6364 6362.982227598556 26.552714366252626 -0.7937724014436753 Angstrom
17.07970432723448 km / s

3161-5311 1d_n3.0060.fit
[OI] 5577 5576.50485002966 47.67352031135267 -0.8338499703404523 Angstrom
[OI] 6300 6299.275765202289 23.190628920908157 -1.0282347977108657 Angstrom
11.523665176115003 km / s

2869-4644 1d_n3.0063.fit
[OI] 5577 5576.737364018324 99.43003251696825 -0.6013359816761294 Angstrom
[OI] 6300 6298.705255190934 22.14099907458835 -1.5987448090663747 Angstrom
[OI] 6364 6362.7035614355755 18.975028517107976 -1.072438564424374 Angstrom
31.80490594382661 km / s

2467-3726 1d_n3.0065.fit
[OI] 5577 5576.965298625508 233.0754651365168 -0.3734013744924596 Angstrom
[OI] 6300 6299.89858782684 52.36158261885936 -0.40541217315967515 Angstrom
[OI] 6364 6363.553835434259 79.5916305384569 -0.2221645657409681 Angstrom
-0.8967621575939688 km / s

2462-3717 1d_n3.0072.fit
[OI] 5577 5576.606101987117 130.40841888994774 -0.7325980128834999 Angstrom
[OI] 6300 6299.932391359967 59.819714874427746 -0.37160864003271854 Angstrom
9.013629642966094 km / s

HIP38926 1d_n3.0074.fit
[OI] 5577 5576.2059139263965 135.21423397973004 -1.132786073603711 Angstrom
[OI] 6300 6299.383202424117 33.446033290008614 -0.920797575882716 Angstrom
27.77251102559258 km / s

HIP37789 1d_n3.0075.fit
[OI] 5577 5576.526351625809 49.051018673874864 -0.8123483741910604 Angstrom
22.105671391990384 km / s

HIP36993 1d_n3.0076.fit
[OI] 5577 5576.300622090493 84.25954588857276 -1.0380779095075923 Angstrom
[OI] 6300 6298.085627790497 24.490445451924202 -2.2183722095032863 Angstrom
-3.4981431266794942 km / s

HIP38541 1d_n3.0077.fit
[OI] 5577 5576.130770957041 136.4232929798311 -1.207929042959222 Angstrom
[OI] 6300 6298.543069526022 22.758021148586398 -1.7609304739780782 Angstrom
26.45557393537888 km / s

HIP36874 1d_n3.0082.fit
[OI] 5577 5575.904171680473 73.6791993048133 -1.4345283195270895 Angstrom
1.2499887140597252 km / s

HIP41544 1d_n3.0083.fit
[OI] 5577 5576.224298152476 102.2931377770691 -1.1144018475242774 Angstrom
[OI] 6300 6299.34814997091 17.97409711812178 -0.9558500290904703 Angstrom
31.008773783920503 km / s

3410-6100 1d_n3.0091.fit
[OI] 5577 5575.923861286747 263.7800907061697 -1.41483871325363 Angstrom
[OI] 6300 6299.01688384505 54.137577482243536 -1.2871161549501267 Angstrom
[OI] 6364 6362.161545089503 18.175411262747208 -1.6144549104965336 Angstrom
24.6500119249003 km / s

2629-4904 1d_n3.0110.fit
[OI] 5577 5576.037821711251 263.2573762331964 -1.300878288749118 Angstrom
[OI] 6300 6298.853150443337 39.1402458683267 -1.4508495566633428 Angstrom
9.318932699709377 km / s

3101-5156 1d_n3.0124.fit
[OI] 5577 5576.510358263922 182.52529465632514 -0.8283417360780732 Angstrom
[OI] 6300 6299.448796034872 33.01613214566967 -0.8552039651276573 Angstrom
46.3443439494893 km / s

HIP64747 1d_n3.0128.fit
[OI] 5577 5576.354415383203 175.38547389755303 -0.9842846167975949 Angstrom
[OI] 6300 6298.691295815802 24.882068356072846 -1.6127041841982646 Angstrom
[OI] 6364 6363.5723921385825 32.27823298330904 -0.20360786141736753 Angstrom
53.894076137836024 km / s

HIP71284 1d_n3.0139.fit
WARNING: not correcting with sky line
7.677903592273635 km / s

2499-4016 1d_n3.0155.fit
[OI] 5577 5576.258225419652 341.4173693298728 -1.0804745803479818 Angstrom
[OI] 6300 6299.357953026787 33.14431781590361 -0.946046973213015 Angstrom
54.77433480425297 km / s

2499-4016 1d_n3.0157.fit
[OI] 5577 5576.373333701715 348.3739930013364 -0.9653662982855167 Angstrom
[OI] 6300 6298.821605322074 34.38483137796496 -1.482394677926095 Angstrom
[OI] 6364 6361.0378346977 26.31386361732812 -2.7381653022994215 Angstrom
78.42612647011353 km / s

HIP76984 1d_n3.0174.fit
[OI] 5577 5576.396146383426 249.91019127503097 -0.9425536165745143 Angstrom
[OI] 6300 6298.764185333848 31.29758933456595 -1.5398146661518695 Angstrom
[OI] 6364 6363.519593417264 32.50024572719138 -0.256406582735508 Angstrom
31.58272326459469 km / s

HIP77536 1d_n3.0178.fit
[OI] 5577 5575.907354116793 117.59742935113536 -1.4313458832075412 Angstrom
[OI] 6300 6299.131346146165 20.549315903602203 -1.1726538538350724 Angstrom
68.85852622913625 km / s

HIP76899 1d_n3.0179.fit
[OI] 5577 5576.028240739213 194.96029577022355 -1.3104592607869563 Angstrom
[OI] 6300 6298.741624943356 27.146091405799478 -1.5623750566437593 Angstrom
58.36746287778814 km / s

HIP79137 1d_n3.0181.fit
WARNING: not correcting with sky line
-5.553368031731136 km / s

HIP79792 1d_n3.0182.fit
[OI] 5577 5575.99526019933 258.00721604156115 -1.3434398006702395 Angstrom
[OI] 6300 6298.929031770182 32.47738360230352 -1.374968229818478 Angstrom
61.09718631749321 km / s

2928-4758 1d_n3.0185.fit
[OI] 5577 5575.490693012189 56.539030023105155 -1.8480069878114591 Angstrom
68.26093753916557 km / s

HIP80722 1d_n3.0187.fit
[OI] 5577 5575.894965136412 274.7317896720119 -1.443734863588361 Angstrom
[OI] 6300 6298.7203226039555 30.998594734576326 -1.5836773960445498 Angstrom
79.29478063960343 km / s

HIP80837 1d_n3.0190.fit
[OI] 5577 5575.590552557847 109.61269288205133 -1.7481474421529128 Angstrom
71.78043624503242 km / s

HIP80700 1d_n3.0191.fit
[OI] 5577 5575.958168795245 279.6663133046309 -1.3805312047552434 Angstrom
[OI] 6300 6298.70348865582 30.9057321206749 -1.600511344179722 Angstrom
62.775242772258466 km / s

HIP83204 1d_n3.0194.fit
[OI] 5577 5575.982122917935 82.09805794410366 -1.3565770820650869 Angstrom
76.4741149714223 km / s

HIP83489 1d_n3.0195.fit
[OI] 5577 5575.830353287558 137.3342041426926 -1.5083467124422896 Angstrom
[OI] 6300 6299.871794450577 56.43775408502043 -0.4322055494230881 Angstrom
58.79139968679168 km / s

3299-5760 1d_n3.0197.fit
[OI] 5577 5576.130505477496 191.71916688103212 -1.2081945225045274 Angstrom
[OI] 6300 6298.955705836729 33.73603469216931 -1.3482941632710208 Angstrom
78.67304634740184 km / s

HIP87769 1d_n3.0198.fit
[OI] 5577 5575.917820272447 198.45482878592543 -1.420879727553256 Angstrom
[OI] 6300 6299.418391243631 38.53206269305018 -0.885608756369038 Angstrom
43.31924880588073 km / s

HIP86193 1d_n3.0199.fit
[OI] 5577 5575.913521494759 91.47714919725175 -1.4251785052410924 Angstrom
[OI] 6300 6298.081477743779 46.14844410745215 -2.2225222562210547 Angstrom
99.54911865715026 km / s

HIP84781 1d_n3.0200.fit
[OI] 5577 5575.851436474055 150.857218576608 -1.487263525945309 Angstrom
[OI] 6300 6298.957032827813 22.25046184728148 -1.3469671721868508 Angstrom
77.00344045400756 km / s

HIP86013 1d_n3.0201.fit
[OI] 5577 5575.925764384423 191.15979249303157 -1.4129356155772257 Angstrom
[OI] 6300 6298.673163332479 39.35459559912821 -1.63083666752118 Angstrom
57.17411933178603 km / s

HIP88622 1d_n3.0202.fit
[OI] 5577 5575.60391390854 85.81562271900556 -1.7347860914605917 Angstrom
67.20232794074035 km / s

HIP89207 1d_n3.0203.fit
[OI] 5577 5575.651832384792 225.07545345330345 -1.6868676152080297 Angstrom
[OI] 6300 6298.293739496651 38.33679905628242 -2.0102605033489453 Angstrom
75.92770602798988 km / s

HIP90539 1d_n3.0204.fit
[OI] 5577 5575.071381346986 540.9294142608838 -2.2673186530146268 Angstrom
[OI] 6300 6298.021101482912 53.11568198469079 -2.2828985170881424 Angstrom
[OI] 6364 6361.023239742643 17.437102613157894 -2.7527602573563854 Angstrom
92.22070751391615 km / s

HIP92781 1d_n3.0205.fit
[OI] 5577 5575.354752266628 328.73492181683037 -1.9839477333725881 Angstrom
[OI] 6300 6298.040452024699 49.72276169299205 -2.263547975300753 Angstrom
[OI] 6364 6362.418416695253 18.511228101993037 -1.357583304747095 Angstrom
64.1693753483849 km / s

HIP87533 1d_n3.0206.fit
[OI] 5577 5575.097070867885 178.18243020290953 -2.241629132115122 Angstrom
[OI] 6300 6298.032113958131 25.61829912051919 -2.2718860418690383 Angstrom
85.538552565479 km / s

HIP87841 1d_n3.0207.fit
[OI] 5577 5575.60223462788 236.45815951791604 -1.7364653721206196 Angstrom
[OI] 6300 6298.135807538347 53.41669844503032 -2.168192461653234 Angstrom
100.325910419507 km / s

HIP87101 1d_n3.0208.fit
[OI] 5577 5575.734996960224 344.4554021069929 -1.6037030397765193 Angstrom
[OI] 6300 6298.989335713305 61.335245087247635 -1.314664286695006 Angstrom
40.04963667141895 km / s

HIP89589 1d_n3.0209.fit
[OI] 5577 5575.837633395811 219.26622002130412 -1.5010666041889635 Angstrom
[OI] 6300 6298.880651698082 27.91296346398089 -1.4233483019179403 Angstrom
78.08829720642646 km / s

HIP89583 1d_n3.0210.fit
[OI] 5577 5575.586946549022 297.3605039773567 -1.751753450977958 Angstrom
[OI] 6300 6298.489657673218 42.67625252891161 -1.8143423267820253 Angstrom
[OI] 6364 6363.4863995469495 108.76554347058698 -0.2896004530502978 Angstrom
39.24350812429893 km / s

HIP90004 1d_n3.0211.fit
[OI] 5577 5575.513023484027 170.55078488824026 -1.8256765159731003 Angstrom
[OI] 6300 6298.344405798056 31.740300059831004 -1.9595942019441281 Angstrom
77.02768283043356 km / s

HIP88010 1d_n3.0212.fit
[OI] 5577 5575.762183465271 329.3869771544596 -1.5765165347293078 Angstrom
[OI] 6300 6298.694025729074 43.066412284450756 -1.6099742709257043 Angstrom
[OI] 6364 6363.147260364776 24.893292457202733 -0.6287396352236101 Angstrom
35.23196255783034 km / s

HIP83867 1d_n3.0213.fit
[OI] 5577 5576.900102473195 416.6515986699092 -0.438597526805097 Angstrom
[OI] 6300 6299.156141212539 17.799912746551506 -1.147858787460791 Angstrom
60.450840390469395 km / s

HIP83867 1d_n3.0214.fit
[OI] 5577 5576.468264956502 86.49139790821931 -0.8704350434982189 Angstrom
[OI] 6300 6297.8569762010075 21.73636042394779 -2.447023798992632 Angstrom
246.80757025505739 km / s

HIP81580 1d_n3.0215.fit
[OI] 5577 5575.808466887451 26.855874397316054 -1.5302331125494675 Angstrom
100.95729140227992 km / s

HIP81952 1d_n3.0216.fit
[OI] 5577 5576.780757192797 208.56980522477133 -0.5579428072032897 Angstrom
[OI] 6300 6299.571260965259 38.835493369153895 -0.7327390347409164 Angstrom
78.52964744559229 km / s

HIP83601 1d_n3.0217.fit
[OI] 6300 6300.142397667178 30.160598752018167 -0.16160233282244008 Angstrom
65.72906071956383 km / s

HIP80700 1d_n3.0218.fit
[OI] 5577 5577.2136236839015 194.15815708271998 -0.12507631609878445 Angstrom
[OI] 6300 6300.3569420538815 57.34826006081667 0.05294205388145201 Angstrom
56.442225977018 km / s

HIP79792 1d_n3.0219.fit
[OI] 5577 5576.913336549435 130.68169413028556 -0.42536345056487335 Angstrom
[OI] 6300 6300.005513783877 44.249138174281605 -0.29848621612291026 Angstrom
65.6590186888412 km / s

HIP79137 1d_n3.0220.fit
WARNING: not correcting with sky line
43.08092476157962 km / s

HIP77536 1d_n3.0222.fit
[OI] 5577 5577.055613696608 85.74825219281857 -0.28308630339233787 Angstrom
[OI] 6300 6299.539601980193 19.313925347080175 -0.7643980198072313 Angstrom
63.867008483229256 km / s

HIP18833 1d_n4.0022.fit
[OI] 6300 6300.2045661411585 155.45976460639096 -0.09943385884162126 Angstrom
[OI] 6364 6363.666892970251 46.03556064794037 -0.10910702974888409 Angstrom
-30.507901290905618 km / s

HIP22349 1d_n4.0023.fit
[OI] 5577 5577.269727140452 67.03920498290431 -0.06897285954801191 Angstrom
[OI] 6300 6300.61284866217 75.86782475897824 0.30884866217002127 Angstrom
[OI] 6364 6363.60831865483 23.688216687657825 -0.1676813451695125 Angstrom
-11.339391236024284 km / s

HIP28159 1d_n4.0024.fit
[OI] 5577 5577.121765789597 91.39953904029403 -0.2169342104034513 Angstrom
[OI] 6300 6301.202324733847 131.69184101443855 0.8983247338464935 Angstrom
[OI] 6364 6364.1894025341 43.22582423883571 0.41340253409998695 Angstrom
-16.112294755705125 km / s

HIP28066 1d_n4.0026.fit
[OI] 5577 5577.19599992616 22.74501065124863 -0.14270007384038763 Angstrom
[OI] 6300 6301.262701529393 43.50093796734618 0.9587015293927834 Angstrom
-39.799026259726816 km / s

HIP28044 1d_n4.0027.fit
[OI] 5577 5576.877584143935 108.35421993608944 -0.46111585606558947 Angstrom
[OI] 6300 6300.907183664856 170.38312081045441 0.6031836648562603 Angstrom
[OI] 6364 6364.173776913094 55.986335071109956 0.3977769130942761 Angstrom
-21.99985488845114 km / s

HIP25905 1d_n4.0028.fit
[OI] 5577 5577.282042091913 103.55624649923917 -0.05665790808689053 Angstrom
[OI] 6300 6301.308107725504 184.19641539917987 1.0041077255036726 Angstrom
[OI] 6364 6364.4888358711 65.02300302728895 0.7128358710997418 Angstrom
-38.67982983073937 km / s

HIP24819 1d_n4.0029.fit
[OI] 5577 5577.953854067241 67.01664524857964 0.6151540672408373 Angstrom
[OI] 6300 6301.900209238891 105.33195268973779 1.5962092388908786 Angstrom
[OI] 6364 6365.002055161131 33.053508269076936 1.2260551611307164 Angstrom
-28.05949854440766 km / s

HIP23941 1d_n4.0031.fit
WARNING: not correcting with sky line
51.7908304107643 km / s

HIP22336 1d_n4.0032.fit
WARNING: not correcting with sky line
58.68361112200188 km / s

HIP24037 1d_n4.0033.fit
[OI] 5577 5578.616041721906 97.11146557484399 1.277341721905941 Angstrom
[OI] 6300 6302.328577973731 160.97611170478024 2.0245779737306293 Angstrom
[OI] 6364 6365.450897760229 49.122642122213506 1.6748977602292143 Angstrom
-25.169695419284153 km / s

1585-1992 1d_n4.0052.fit
[OI] 5577 5577.990728117048 129.89425955189782 0.6520281170478484 Angstrom
-9.231949214279439 km / s

1678-6580 1d_n4.0055.fit
[OI] 5577 5576.875717690952 141.08367061540795 -0.46298230904812954 Angstrom
[OI] 6300 6301.293967023708 18.534649841299444 0.9899670237082319 Angstrom
-3.9323658069404956 km / s

1955-2709 1d_n4.0057.fit
[OI] 5577 5576.582463212374 191.06066279650398 -0.7562367876262215 Angstrom
4.638090085090161 km / s

1509-1819 1d_n4.0060.fit
[OI] 5577 5575.983431026326 92.62850714357687 -1.355268973674356 Angstrom
8.475863360549347 km / s

1852-2500 1d_n4.0073.fit
[OI] 5577 5575.391913258075 271.25100650799 -1.9467867419252798 Angstrom
39.06330189578742 km / s

645-4956 1d_n4.0089.fit
[OI] 5577 5576.0036358246925 232.5297407881708 -1.335064175307707 Angstrom
17.57625267946979 km / s

HIP61619 1d_n4.0101.fit
[OI] 5577 5575.884701235264 203.6376018757275 -1.453998764735843 Angstrom
[OI] 6300 6298.115208815984 35.06986713536804 -2.1887911840158267 Angstrom
37.808963439069835 km / s

1257-4987 1d_n4.0103.fit
[OI] 5577 5575.956749853628 466.2775374898002 -1.381950146372219 Angstrom
[OI] 6300 6300.060956105869 19.398125196805452 -0.2430438941310058 Angstrom
-23.841259500376815 km / s

1530-4571 1d_n4.0105.fit
[OI] 5577 5575.6375444889945 316.3061700360468 -1.7011555110057088 Angstrom
[OI] 6300 6300.0082737867415 88.24326995436454 -0.2957262132586038 Angstrom
11.20107112312207 km / s

1992-3265 1d_n4.0115.fit
[OI] 5577 5575.510304397854 321.9720145660946 -1.8283956021459744 Angstrom
[OI] 6300 6299.422077491435 31.698982949081927 -0.881922508565367 Angstrom
3.079198995436723 km / s

1992-3265-1 1d_n4.0117.fit
[OI] 5577 5575.927041612624 732.1347572131272 -1.4116583873765194 Angstrom
[OI] 6300 6298.663489071395 26.72476678987377 -1.640510928605181 Angstrom
-19.20388453346965 km / s

10-363746 1d_n4.0121.fit
[OI] 5577 5575.635262732394 61.47476267877521 -1.7034372676062048 Angstrom
-6.324265107857488 km / s

10-371172 1d_n4.0131.fit
[OI] 5577 5575.823970606519 148.31879482478166 -1.5147293934815025 Angstrom
[OI] 6300 6299.967206928874 28.378019380453022 -0.3367930711265217 Angstrom
-16.87084306642326 km / s

10-383447 1d_n4.0137.fit
[OI] 5577 5575.654719668008 284.8962977157869 -1.6839803319926432 Angstrom
[OI] 6300 6299.3215798702295 36.57035396262098 -0.9824201297706168 Angstrom
-33.02243611950552 km / s

10-338405 1d_n4.0146.fit
[OI] 5577 5575.781502639964 253.7374200963756 -1.557197360036298 Angstrom
[OI] 6300 6299.919874159994 21.558806950258944 -0.38412584000616334 Angstrom
11.76462630371907 km / s

10-338405 1d_n4.0147.fit
[OI] 5577 5575.762542493464 193.70518035038296 -1.5761575065362194 Angstrom
[OI] 6300 6300.008121749061 28.50035828010605 -0.29587825093949505 Angstrom
-25.366172317241933 km / s

10-369977 1d_n4.0161.fit
[OI] 5577 5575.631089469141 177.2302761110379 -1.7076105308588012 Angstrom
64.15317393732212 km / s

1139-1219 1d_n4.0179.fit
[OI] 5577 5575.973893079973 698.4376382085741 -1.3648069200271493 Angstrom
[OI] 6300 6299.753990207248 23.359667027000558 -0.5500097927524621 Angstrom
-6.959410449475641 km / s

HIP85007 1d_n4.0189.fit
[OI] 5577 5575.721727525667 101.20523092970919 -1.6169724743331244 Angstrom
40.54112366563105 km / s

HIP88945 1d_n4.0192.fit
[OI] 5577 5575.839922782724 79.02488491127131 -1.4987772172762561 Angstrom
30.670445642376016 km / s

HIP92270 1d_n4.0194.fit
[OI] 5577 5575.867063123676 80.15658496448992 -1.471636876323828 Angstrom
17.618814178381804 km / s

344-10011 1d_n4.0196.fit
[OI] 5577 5575.567694353425 277.0174359982207 -1.7710056465748494 Angstrom
56.85770114723869 km / s

HIP84905 1d_n4.0203.fit
[OI] 5577 5575.970039520622 79.84955793086058 -1.3686604793783772 Angstrom
44.59825441107469 km / s

HIP85042 1d_n4.0204.fit
[OI] 5577 5575.793037481542 35.257271819751146 -1.5456625184579025 Angstrom
44.53096178637332 km / s

HIP85757 1d_n4.0205.fit
[OI] 5577 5575.567946594559 235.3895638735184 -1.77075340544161 Angstrom
58.32153095796505 km / s

HIP85963 1d_n4.0206.fit
[OI] 5577 5575.6010003695465 143.44282680895833 -1.7376996304537897 Angstrom
48.49144597571177 km / s

HIP82588 1d_n4.0208.fit
[OI] 5577 5575.358219105161 38.21484132660784 -1.9804808948392747 Angstrom
70.39008145001267 km / s

HIP81461 1d_n4.0209.fit
[OI] 5577 5575.973799514774 233.63166978603792 -1.3649004852259168 Angstrom
[OI] 6300 6299.982549774853 37.953499577287644 -0.3214502251466911 Angstrom
16.207873241333814 km / s

HIP81300 1d_n4.0210.fit
[OI] 5577 5575.867308347588 25.63260518215758 -1.4713916524124215 Angstrom
36.57360226599582 km / s

HIP90365 1d_n4.0211.fit
[OI] 5577 5575.700186972792 214.9393826939538 -1.6385130272083188 Angstrom
33.138757760204655 km / s

HIP93377 1d_n4.0212.fit
[OI] 5577 5575.623337461627 195.49615475312868 -1.7153625383734834 Angstrom
[OI] 6300 6299.943655241614 40.80792505747629 -0.3603447583864181 Angstrom
-15.708065341488044 km / s

HIP93966 1d_n4.0213.fit
[OI] 5577 5575.665671940527 36.77158999901292 -1.6730280594729265 Angstrom
44.432502004111434 km / s

HIP95447 1d_n4.0215.fit
[OI] 5577 5575.707260169967 17.471681862223374 -1.631439830032832 Angstrom
-1.0542183211373128 km / s

HIP93185 1d_n4.0216.fit
[OI] 5577 5575.929585709261 209.09595632843286 -1.4091142907391259 Angstrom
[OI] 6300 6299.3762723058435 22.230089194047064 -0.9277276941566015 Angstrom
2.761179041733101 km / s

4461-9858 1d_n5.0047.fit
[OI] 5577 5577.019090709665 219.8888446021855 -0.31960929033539287 Angstrom
[OI] 6300 6300.233089000973 31.48715782515689 -0.070910999026637 Angstrom
-26.466077283925777 km / s

4461-9957 1d_n5.0048.fit
[OI] 5577 5577.041849958422 71.88929064943761 -0.29685004157818184 Angstrom
[OI] 6300 6300.848261312215 29.66938351538188 0.5442613122149851 Angstrom
-87.1111745147644 km / s

3697-6922 1d_n5.0051.fit
WARNING: not correcting with sky line
-39.713247108267566 km / s

3969-8156 1d_n5.0061.fit
WARNING: not correcting with sky line
-28.865677111563617 km / s

3429-9285 1d_n5.0064.fit
[OI] 5577 5577.257000806478 269.0976028519681 -0.08169919352258148 Angstrom
[OI] 6300 6300.138148889998 52.77075468737804 -0.1658511100022224 Angstrom
[OI] 6364 6364.9201051772725 22.81613641713993 1.1441051772726496 Angstrom
-39.92610439313569 km / s

4373-9376 1d_n5.0081.fit
[OI] 5577 5577.0078426080645 984.4674385890813 -0.33085739193575137 Angstrom
[OI] 6300 6298.790016847558 16.715173653754523 -1.5139831524420515 Angstrom
17.02721005357815 km / s

4373-9376 1d_n5.0082.fit
[OI] 5577 5576.859656662066 204.24718824054315 -0.4790433379339447 Angstrom
[OI] 6300 6299.770010429338 23.45956675339408 -0.5339895706620155 Angstrom
-36.71858939094024 km / s

3060-5058 1d_n5.0096.fit
[OI] 5577 5576.942974934604 177.8141742161606 -0.39572506539661845 Angstrom
[OI] 6300 6299.4174530002365 19.25244263065526 -0.8865469997635955 Angstrom
-12.572318868198058 km / s

3652-6770 1d_n5.0099.fit
[OI] 5577 5576.954274926032 285.6212563842412 -0.3844250739684867 Angstrom
[OI] 6300 6299.346399916939 17.20167545384135 -0.957600083061152 Angstrom
[OI] 6364 6363.130884494609 18.131857590172803 -0.6451155053910043 Angstrom
-13.966056225192904 km / s

3555-6511 1d_n5.0100.fit
[OI] 5577 5576.90407349739 172.4275046911495 -0.43462650261062663 Angstrom
[OI] 6300 6300.013625277075 66.19415822052059 -0.29037472292475286 Angstrom
-27.638024369198668 km / s

3555-6844 1d_n5.0101.fit
[OI] 5577 5576.946893363281 157.54409615120932 -0.39180663671959337 Angstrom
-20.89544567744121 km / s

4283-9127 1d_n5.0112.fit
[OI] 6300 6299.809861569584 21.474655645634293 -0.49413843041565997 Angstrom
-16.224935301310936 km / s

4361-9837 1d_n5.0116.fit
[OI] 5577 5576.657769399586 195.1399862572858 -0.6809306004142854 Angstrom
[OI] 6300 6299.574854589516 29.554421666757552 -0.7291454104843069 Angstrom
[OI] 6364 6361.061372240161 24.48103070609853 -2.7146277598385495 Angstrom
15.750853734833917 km / s

3024-10309 1d_n5.0122.fit
[OI] 5577 5576.975082516651 1159.992055939809 -0.3636174833491168 Angstrom
[OI] 6300 6299.915184986346 53.553124377487265 -0.38881501365449367 Angstrom
-21.94779870516083 km / s

3687-6871 1d_n5.0130.fit
[OI] 5577 5576.961704868804 963.6074556969236 -0.376995131196054 Angstrom
-28.826233332477656 km / s

4025-10198 1d_n5.0135.fit
[OI] 5577 5576.897299281275 218.87193762886108 -0.44140071872516273 Angstrom
[OI] 6300 6299.985893182564 36.128300263188095 -0.31810681743627356 Angstrom
-15.098680214908086 km / s

4420-9849 1d_n5.0137.fit
[OI] 5577 5576.919126729568 279.1478964796882 -0.41957327043201076 Angstrom
[OI] 6300 6298.962972081658 19.080046836102277 -1.3410279183417515 Angstrom
1.9665004294582715 km / s

2647-4136 1d_n5.0171.fit
[OI] 5577 5575.728998018187 245.6022561765124 -1.6097019818134868 Angstrom
-0.20270362489056915 km / s

HIP96258 1d_n5.0217.fit
WARNING: not correcting with sky line
-61.00786107057686 km / s

HIP93185 1d_n5.0218.fit
[OI] 5577 5575.837316188853 69.46211674916393 -1.5013838111472069 Angstrom
17.80104514034076 km / s

HIP74537 1d_n5.0220.fit
[OI] 5577 5577.416684785397 86.3610457353661 0.07798478539643838 Angstrom
20.992056204208666 km / s

HIP74079 1d_n5.0221.fit
[OI] 5577 5577.574606684473 302.4806040472405 0.2359066844728659 Angstrom
[OI] 6300 6300.044251028566 104.51444773619068 -0.2597489714344192 Angstrom
[OI] 6364 6365.127814395314 22.609657429340633 1.3518143953142499 Angstrom
8.326442189554626 km / s

HIP74033 1d_n5.0222.fit
[OI] 5577 5577.635060500529 251.00303882113272 0.29636050052886276 Angstrom
[OI] 6300 6300.193533780803 44.22766564075706 -0.11046621919740574 Angstrom
22.936613272979827 km / s

HIP72479 1d_n5.0223.fit
[OI] 5577 5577.661161780863 334.7289956289136 0.3224617808627954 Angstrom
15.238676146473153 km / s


In [7]:
raw_rv = raw_offsets / Halpha * c.to(u.km/u.s)

This is just total insanity


In [8]:
grid = np.linspace(0, 14, 256)

diff = all_sky_offsets[:,0] - ((raw_rv - true_rv)/c*5577*u.angstrom).decompose()
# diff = all_sky_offsets[:,0] - ((meas_rv - true_rv)/c*5577*u.angstrom).decompose()
diff[np.abs(diff) > 2*u.angstrom] = np.nan * u.angstrom

night_polys = dict()
for n in range(1,5+1):
    mask = (night_id == n) & np.isfinite(diff)
    coef = np.polyfit(obs_time[mask], diff[mask], deg=1, w=np.full(mask.sum(), 1/0.1))
    poly = np.poly1d(coef)
    night_polys[n] = poly
    
    plt.figure()
    sc = plt.scatter(obs_time[mask], diff[mask])
    plt.plot(grid, poly(grid), color=sc.get_facecolor()[0], marker='')
    plt.title('n{}'.format(n))
    plt.xlim(-0, 15)
    plt.ylim(-2, 2)


/Users/adrian/anaconda/envs/comoving-rv/lib/python3.6/site-packages/ipykernel/__main__.py:5: RuntimeWarning: invalid value encountered in greater

In [13]:
corrected_rv = np.zeros(len(observations)) * u.km/u.s
derps = dict()
for n in np.unique(night_id):
    mask = night_id == n
    
    sky_offset = np.nanmean(all_sky_offsets[mask,:2], axis=1)
    sky_offset[np.isnan(sky_offset)] = 0.*u.angstrom
    sky_offset -= night_polys[n](obs_time[mask]) * u.angstrom
    
    corrected_rv[mask] = (raw_offsets[mask] - sky_offset) / Halpha * c.to(u.km/u.s)
    
for n in np.unique(night_id):
    mask = night_id == n
    derps[n] = np.nanmedian(corrected_rv[mask] - true_rv[mask])
    corrected_rv[mask] = corrected_rv[mask] - derps[n]


/Users/adrian/anaconda/envs/comoving-rv/lib/python3.6/site-packages/ipykernel/__main__.py:6: RuntimeWarning: Mean of empty slice

In [14]:
fig,axes = plt.subplots(2, 5, figsize=(15,6.5), sharex='row', sharey='row')

_lim = (-200, 200)
_grid = np.linspace(_lim[0], _lim[1], 16)
_bins = np.linspace(-100, 100, 31)
for n in range(1,5+1):
    ax = axes[0, n-1]
    
    mask = night_id == n
    
    cb = ax.scatter(corrected_rv[mask], true_rv[mask], c=color_by[mask], 
                    marker='.', alpha=0.75, vmin=0, vmax=12, linewidth=1., 
                    edgecolor='#aaaaaa', s=50)
    ax.plot(_grid, _grid, marker='', zorder=-10, color='#888888')
    
    # histogram
    ax = axes[1, n-1]
    drv = corrected_rv[mask] - true_rv[mask]
    ax.hist(drv, bins=_bins)
    
    print(n, np.median(drv), 1.5 * np.median(np.abs(drv - np.median(drv))), np.mean(drv), np.std(drv))
    
axes[0,0].set_xlim(_lim)
axes[0,0].set_ylim(_lim)
axes[1,0].set_xlim(_bins.min(), _bins.max())

fig.tight_layout()

# all:
drv = corrected_rv - true_rv
print('total:', 1.5 * np.median(np.abs(drv - np.median(drv))), np.std(drv[np.abs(drv) < 50*u.km/u.s]))

plt.figure()
plt.hist(drv[np.abs(drv) < 100*u.km/u.s], bins='auto');


1 3.552713678800501e-15 km / s 12.83077081667797 km / s -5.317430253290531 km / s 18.083199736948277 km / s
2 8.881784197001252e-16 km / s 20.306247108750355 km / s -0.4991440179204223 km / s 20.50983159447169 km / s
3 0.0 km / s 14.26683123491049 km / s -27.64453876962776 km / s 218.49511498352547 km / s
4 -1.7763568394002505e-15 km / s 26.645769129012287 km / s 0.5074959045001305 km / s 27.269025595406166 km / s
5 0.0 km / s 9.964899061285529 km / s -3.9240886144039187 km / s 20.661927588327057 km / s
total: 16.740214555302778 km / s 17.40339460245057 km / s

In [21]:
fig,axes = plt.subplots(1, 2, figsize=(6.5, 3.1))

_lim = (-200, 200)
_grid = np.linspace(_lim[0], _lim[1], 16)
_bins = np.linspace(-100, 100, 31)

cb = axes[0].scatter(corrected_rv, true_rv, c=color_by, 
                     marker='.', alpha=0.75, vmin=0, vmax=12, linewidth=1., 
                     edgecolor='#aaaaaa', s=50)
axes[0].plot(_grid, _grid, marker='', zorder=-10, color='#888888')

# histogram
drv = corrected_rv - true_rv
axes[1].hist(drv, bins=_bins)

print(n, np.median(drv), 1.5 * np.median(np.abs(drv - np.median(drv))), np.mean(drv), np.std(drv))
    
axes[0].set_xlim(_lim)
axes[0].set_ylim(_lim)
axes[1].set_xlim(_bins.min(), _bins.max())

fig.tight_layout()


5 0.0 km / s 16.740214555302778 km / s -11.737585669126542 km / s 138.91381527354295 km / s

Do same as above, as a class


In [25]:
class RVCorrector(object):

    def __init__(self, session, run_name):
        self.session = session
        self.run_name = str(run_name)

        # get wavelength for Halpha
        self.Halpha, = session.query(SpectralLineInfo.wavelength)\
                              .filter(SpectralLineInfo.name == 'Halpha').one()

        self._compute_offset_corrections()

    def _compute_offset_corrections(self):
        session = self.session
        run_name = self.run_name

        q = session.query(Observation).join(Run, SpectralLineMeasurement, PriorRV)
        q = q.filter(Run.name == run_name)
        q = q.filter(SpectralLineMeasurement.x0 != None)
        q = q.filter(PriorRV.rv != None)
        logger.debug('{0} observations with prior RV measurements'
                     .format(q.distinct().count()))

        # retrieve all observations with measured centroids and previous RV's
        observations = q.all()

        # What we do below is look at the residual offsets between applying a naïve
        # sky-line correction and the true RV (with the barycentric velocity
        # applied)

        raw_offsets = np.zeros(len(observations)) * u.angstrom
        all_sky_offsets = np.full((len(observations), 3), np.nan) * u.angstrom
        true_rv = np.zeros(len(observations)) * u.km/u.s
        obs_time = np.zeros(len(observations))
        night_id = np.zeros(len(observations), dtype=int)
        corrected_rv = np.zeros(len(observations)) * u.km/u.s

        for i,obs in enumerate(observations):
            # convert obstime into decimal hour
            obs_time[i] = np.sum(np.array(list(map(float, obs.time_obs.split(':')))) / np.array([1., 60., 3600.]))

            # Compute the raw offset: difference between Halpha centroid and true
            # wavelength value
            x0 = obs.measurements[0].x0 * u.angstrom
            offset = (x0 - self.Halpha)
            raw_offsets[i] = offset

            night_id[i] = obs.night

            # For each sky line (that passes certain quality checks), compute the
            # offset between the predicted wavelength and measured centroid
            # TODO: generalize these quality cuts - see also below in
            # get_corrected_rv
            sky_offsets = []
            for j,meas in enumerate(obs.measurements[1:]):
                sky_offset = meas.x0*u.angstrom - meas.info.wavelength
                if (meas.amp > 16 and meas.std_G < 2 and meas.std_G > 0.3 and
                        np.abs(sky_offset) < 4*u.angstrom): # MAGIC NUMBER: quality cuts
                    sky_offsets.append(sky_offset)
                    all_sky_offsets[i,j] = sky_offset

            sky_offsets = u.Quantity(sky_offsets)

            if len(sky_offsets) > 0:
                sky_offset = np.mean(sky_offsets)
            else:
                sky_offset = np.nan * u.angstrom
                logger.debug("not correcting with sky line for {0}".format(obs))

            true_rv[i] = obs.prior_rv.rv - obs.v_bary

        raw_rv = raw_offsets / self.Halpha * c.to(u.km/u.s)

        # unique night ID's
        unq_night_id = np.unique(night_id)
        unq_night_id.sort()

        # Now we do a totally insane thing. From visualizing the residual
        # differences, there seems to be a trend with the observation time. We
        # fit a line to these residuals and use this to further correct the
        # wavelength solutions using just the (strongest) [OI] 5577 Å line.
        diff = all_sky_offsets[:,0] - ((raw_rv - true_rv)/c*5577*u.angstrom).decompose()
        diff[np.abs(diff) > 2*u.angstrom] = np.nan * u.angstrom # reject BIG offsets

        self._night_polys = dict()
        self._night_final_offsets = dict()
        for n in unq_night_id:
            mask = (night_id == n) & np.isfinite(diff)
            coef = np.polyfit(obs_time[mask], diff[mask], deg=1, w=np.full(mask.sum(), 1/0.1))
            poly = np.poly1d(coef)
            self._night_polys[n] = poly

            sky_offset = np.nanmean(all_sky_offsets[mask,:2], axis=1)
            sky_offset[np.isnan(sky_offset)] = 0.*u.angstrom
            sky_offset -= self._night_polys[n](obs_time[mask]) * u.angstrom

            corrected_rv[mask] = (raw_offsets[mask] - sky_offset) / self.Halpha * c.to(u.km/u.s)

            # Finally, we align the median of each night's ∆RV distribution with 0
            drv = corrected_rv[mask] - true_rv[mask]
            self._night_final_offsets[n] = np.nanmedian(drv)

        # now estimate the std. dev. uncertainty using the MAD
        all_drv = corrected_rv - true_rv
        self._abs_err = 1.5 * np.nanmedian(np.abs(all_drv - np.nanmedian(all_drv)))

    def get_corrected_rv(self, obs):
        """Compute a corrected radial velocity for the given observation"""

        # Compute the raw offset: difference between Halpha centroid and true
        # wavelength value
        x0 = obs.measurements[0].x0 * u.angstrom
        raw_offset = (x0 - self.Halpha)

        # precision estimate from line centroid error
        precision = (obs.measurements[0].x0_error* u.angstrom) / self.Halpha * c.to(u.km/u.s)

        # For each sky line (that passes certain quality checks), compute the
        # offset between the predicted wavelength and measured centroid
        # TODO: generalize these quality cuts - see also above in
        # _compute_offset_corrections
        sky_offsets = np.full(3, np.nan) * u.angstrom
        for j,meas in enumerate(obs.measurements[1:]):
            sky_offset = meas.x0*u.angstrom - meas.info.wavelength
            if (meas.amp > 16 and meas.std_G < 2 and meas.std_G > 0.3 and
                    np.abs(sky_offset) < 3.3*u.angstrom): # MAGIC NUMBER: quality cuts
                sky_offsets[j] = sky_offset

        # final sky offset to apply
        flag = 0
        sky_offset = np.nanmean(sky_offsets)
        if np.isnan(sky_offset.value):
            logger.debug("not correcting with sky line for {0}".format(obs))
            sky_offset = 0*u.angstrom
            flag = 1

        # apply global sky offset correction - see _compute_offset_corrections()
        sky_offset -= self._night_polys[obs.night](obs.utc_hour) * u.angstrom

        # compute radial velocity and correct for sky line
        rv = (raw_offset - sky_offset) / self.Halpha * c.to(u.km/u.s)

        # correct for offset of median of ∆RV distribution from targets with
        # prior/known RV's
        rv -= self._night_final_offsets[obs.night]

        # rv error
        err = np.sqrt(self._abs_err**2 + precision**2)

        return rv, err, flag

In [26]:
rv_corr = RVCorrector(session, 'mdm-spring-2017')


/Users/adrian/anaconda/envs/comoving-rv/lib/python3.6/site-packages/ipykernel/__main__.py:82: RuntimeWarning: invalid value encountered in greater

In [27]:
corrected_rv2 = np.zeros_like(corrected_rv)
err = np.zeros_like(corrected_rv)
flags = np.zeros(len(corrected_rv2))
for i,obs in enumerate(observations):
    corrected_rv2[i], err[i], flags[i] = rv_corr.get_corrected_rv(obs)


/Users/adrian/anaconda/envs/comoving-rv/lib/python3.6/site-packages/ipykernel/__main__.py:130: RuntimeWarning: Mean of empty slice

In [28]:
fig,axes = plt.subplots(2, 5, figsize=(15,6.5), sharex='row', sharey='row')

_lim = (-200, 200)
_grid = np.linspace(_lim[0], _lim[1], 16)
_bins = np.linspace(-100, 100, 31)
for n in range(1,5+1):
    ax = axes[0, n-1]
    
    mask = (night_id == n) & (flags == 0)
    cb = ax.scatter(corrected_rv2[mask], true_rv[mask], c=color_by[mask], 
                   marker='.', alpha=0.75, vmin=0, vmax=12, linewidth=1., 
                    edgecolor='#aaaaaa', s=50)
    ax.plot(_grid, _grid, marker='', zorder=-10, color='#888888')
    
    # histogram
    ax = axes[1, n-1]
    drv = corrected_rv2[mask] - true_rv[mask]
    ax.hist(drv, bins=_bins)
    
    print(n, np.median(drv), 1.5 * np.median(np.abs(drv - np.median(drv))), np.mean(drv), np.std(drv))
    
axes[0,0].set_xlim(_lim)
axes[0,0].set_ylim(_lim)
axes[1,0].set_xlim(_bins.min(), _bins.max())

fig.tight_layout()

# all:
drv = corrected_rv2 - true_rv
print('total:', 1.5 * np.median(np.abs(drv - np.median(drv))), np.std(drv[np.abs(drv) < 50*u.km/u.s]))

plt.figure()
plt.hist(drv[np.abs(drv) < 100*u.km/u.s], bins='auto');


1 3.552713678800501e-15 km / s 15.434787892158447 km / s -5.188222367473427 km / s 19.161373965875086 km / s
2 -5.101770897317557 km / s 13.947159816416535 km / s -3.893017368133714 km / s 12.472618569546832 km / s
3 -0.593009319040096 km / s 14.17957139123754 km / s -28.259440367420815 km / s 223.97320731438626 km / s
4 -0.17479452574242416 km / s 23.193839957437547 km / s -2.653981911852103 km / s 23.35512518456692 km / s
5 -2.458896551060519 km / s 11.794232183910378 km / s -3.7664104748911704 km / s 18.296199177940256 km / s
total: 17.69966812706215 km / s 17.54436628620426 km / s

In [30]:
fig,axes = plt.subplots(1, 2, figsize=(6.5, 3.1))

_lim = (-200, 200)
_grid = np.linspace(_lim[0], _lim[1], 16)
_bins = np.linspace(-100, 100, 31)

# cb = axes[0].scatter(corrected_rv2, true_rv, c=color_by, 
#                      marker='.', alpha=0.75, vmin=0, vmax=12, linewidth=1., 
#                      edgecolor='#aaaaaa', s=50)
cb = axes[0].scatter(corrected_rv2, true_rv, 
                     marker='.', alpha=0.75, linewidth=1., 
                     edgecolor='#aaaaaa', s=50)
axes[0].plot(_grid, _grid, marker='', zorder=-10, color='#888888')

# histogram
drv = corrected_rv2 - true_rv
axes[1].hist(drv, bins=_bins)

print(n, np.median(drv), 1.5 * np.median(np.abs(drv - np.median(drv))), np.mean(drv), np.std(drv))
    
axes[0].set_xlim(_lim)
axes[0].set_ylim(_lim)
axes[1].set_xlim(_bins.min(), _bins.max())

fig.tight_layout()


5 -2.458896551060519 km / s 17.69966812706215 km / s -13.416429930713512 km / s 138.92575908864254 km / s

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [128]:
fig,axes = plt.subplots(3, 5, figsize=(15,8), sharex=True, sharey=True)

for n in range(1,5+1):
    ax = axes[:,n-1]
    mask = night_id == n
    
    for j in range(3):
        ax[j].scatter(obs_time[mask], all_sky_offsets[mask,j], c=np.arange(mask.sum()), cmap='jet')
    
    ax[0].set_title('night {0}'.format(n))

axes[2,2].set_xlabel('time [hour]')
axes[0,0].set_ylabel('5577 offset')
axes[1,0].set_ylabel('6300 offset')
axes[2,0].set_ylabel('6364 offset')

fig.tight_layout()

# ---

fig,axes = plt.subplots(3, 5, figsize=(15,8), sharex=True, sharey=True)

for n in range(1,5+1):
    ax = axes[:,n-1]
    mask = night_id == n
    
    for j in range(3):
#         ax[j].scatter(hour_angle[mask], all_sky_offsets[mask,j], c=np.arange(mask.sum()), cmap='jet')
        ax[j].scatter(hour_angle[mask], all_sky_offsets[mask,j], c=airmass[mask], 
                      cmap='Spectral', edgecolor='#555555', linewidth=1)
    
    ax[0].set_title('night {0}'.format(n))

axes[2,2].set_xlabel('time [hour]')
axes[0,0].set_ylabel('5577 offset')
axes[1,0].set_ylabel('6300 offset')
axes[2,0].set_ylabel('6364 offset')

axes[0,0].set_xlim(-75, 75)

fig.tight_layout()



In [138]:
((raw_rv - true_rv)[mask]/c*5577).decompose()


Out[138]:
$[0.20122337,~0.043862438,~0.29430355,~0.23662698,~0.13507043,~-0.41822142,~-0.52730527,~-0.3262756,~-0.50606495,~-0.61870854,~-0.66404279,~-0.67688848,~-0.72295722,~-0.85432606,~-1.098773,~-0.84358084,~-0.8218817,~-0.93172964,~-1.2129257,~-1.3529247,~-1.5656979,~-2.5285209,~-0.39260151,~-0.26459427,~-0.24483261,~-0.12367286] \; \mathrm{}$

In [145]:
plt.figure(figsize=(6,6))

for n in range(1,5+1):
    mask = night_id == n
    plt.scatter(all_sky_offsets[mask,0], 
                ((raw_rv - true_rv)[mask]/c*5577).decompose())
plt.xlim(-2.5,2.5)
# plt.ylim(-150,150)
plt.ylim(-2.5,2.5)


Out[145]:
(-2.5, 2.5)

In [ ]:


Take a peek at velocity differences


In [71]:
q = session.query(Observation.group_id).join(Run, SpectralLineMeasurement)
q = q.filter(Run.name == 'mdm-spring-2017')
print(q.distinct().count())
gids = np.array([gid for gid, in q.distinct().all()])


288

In [74]:
rv_diffs = []
all_x0s = []
for gid in gids:
    meas_q = session.query(SpectralLineMeasurement).join(Observation, SpectralLineInfo)
    meas_q = meas_q.filter(SpectralLineInfo.name == 'Halpha')
    meas_q = meas_q.filter(Observation.group_id == gid)
    x0s = [meas.x0 for meas in meas_q.all()]
    
    if len(x0s) == 2:
        rv_diff = (x0s[1] - x0s[0])*u.angstrom / Halpha * c.to(u.km/u.s)
        rv_diffs.append(rv_diff)
        
        all_x0s = all_x0s + x0s
    
    else:
        rv_diffs.append(np.nan*u.km/u.s)
        
rv_diffs = u.Quantity(rv_diffs)
all_x0s = u.Quantity(all_x0s)

In [75]:
# random shuffle
# TODO: redo this after correcting for barycenter, sky shifts, etc.
_derp = np.random.choice(len(all_x0s), size=len(all_x0s), replace=False)
random_rv_diff = (all_x0s - all_x0s[_derp])*u.angstrom / Halpha * c.to(u.km/u.s)

In [76]:
_,bins,_ = plt.hist(rv_diffs[np.isfinite(rv_diffs)], bins=np.linspace(-150, 150, 31), 
                    normed=True, alpha=0.5)

plt.hist(np.random.normal(0, np.sqrt(25**2 + 25**2 + 5**2 + 5**2), size=64000), 
         bins=bins, normed=True, alpha=0.5);

_,bins,_ = plt.hist(random_rv_diff[np.isfinite(random_rv_diff)], bins=bins, normed=True, alpha=0.4)


Now look at differences in sky lines


In [48]:
sky_diffs = []
for gid in gids:
    meas_q = session.query(SpectralLineMeasurement).join(Observation, SpectralLineInfo)
    meas_q = meas_q.filter(SpectralLineInfo.name == '[OI] 6300')
    meas_q = meas_q.filter(Observation.group_id == gid)
    x0s = [meas.x0 for meas in meas_q.all()]

    if len(x0s) == 2:
        sky_diff = (x0s[1] - x0s[0])*u.angstrom / (6300*u.angstrom) * c.to(u.km/u.s)
        sky_diffs.append(sky_diff)
    
    else:
        sky_diffs.append(np.nan*u.km/u.s)
    
sky_diffs = u.Quantity(sky_diffs)

In [49]:
plt.hist(sky_diffs[np.isfinite(sky_diffs)], bins=np.linspace(-50, 50, 30));


TODO: ok, so apparently I need to correct for sky line shifts before doing RV difference??


In [ ]:


In [ ]:


In [18]:
seps = []
for gid in gids[np.isfinite(rv_diffs) & (rv_diffs < 15*u.km/u.s)]:
    _q = session.query(TGASSource).join(Observation)
    _q = _q.filter(Observation.group_id == gid)
    tgas_data = _q.all()
    
    c1 = tgas_data[0].skycoord
    c2 = tgas_data[1].skycoord
    
    sep = c1.separation_3d(c2)
    seps.append(sep)

seps = u.Quantity(seps)


/Users/adrian/anaconda/envs/comoving-rv/lib/python3.6/site-packages/ipykernel/__main__.py:2: RuntimeWarning: invalid value encountered in less
  from ipykernel import kernelapp as app

In [30]:
plt.hist(seps[seps > 0*u.pc], bins='auto')


Out[30]:
(array([  9.,   9.,   8.,  23.,  24.,  23.,  45.,  20.,   3.]),
 array([  0.0785138 ,   1.39608266,   2.71365152,   4.03122038,
          5.34878924,   6.6663581 ,   7.98392696,   9.30149583,
         10.61906469,  11.93663355]),
 <a list of 9 Patch objects>)

In [ ]: